/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.admin.course;

import hibernate.dao.StudentDAO;
import hibernate.entity.Courses;
import hibernate.entity.Students;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import org.primefaces.event.TransferEvent;
import ui.admin.AbstractAdminBean;
import ui.common.AbstractUIBean;
import util.Converter;

/**
 *
 * @author Administrator
 */
@ManagedBean
@ViewScoped
public class CourseStudentManager extends AbstractAdminBean {

    List<Students> sourseStudent, targetStudent;
    StudentDAO stuDao = new StudentDAO();
    Courses currCourse;

    public List<Students> getSourseStudent() {
        return sourseStudent;
    }

    public void setSourseStudent(List<Students> sourseStudent) {
        this.sourseStudent = sourseStudent;
    }

    public List<Students> getTargetStudent() {
        return targetStudent;
    }

    public void setTargetStudent(List<Students> targetStudent) {
        this.targetStudent = targetStudent;
    }

    void loadData() {
        HttpSession session = getHttpSession();
        currCourse = (Courses) session.getAttribute("pickCourse");
        targetStudent = Converter.iteratorToList(currCourse.getStudentses().iterator());
        sourseStudent = stuDao.outOfCourse(currCourse);

    }

    public Courses getCurrCourse() {
        return currCourse;
    }

    public void setCurrCourse(Courses currCourse) {
        this.currCourse = currCourse;
    }

    public CourseStudentManager() {
        isAuthor();
        loadData();
    }

    /**
     * @param TransferEvent an event for transferring student to some course
     * @return void
     */
    public void studentTransfer(TransferEvent event) {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        targetStudent = (List<Students>) session.getAttribute("students");
        if (targetStudent == null) {
            targetStudent = new ArrayList<Students>();
        }
        for (Object item : event.getItems()) {
            int stuID = Integer.parseInt(item.toString());
            Students c = stuDao.readByID(stuID);
            for (Students students : targetStudent) {
            }

//            showPrimeMessage("Result", String.valueOf(targetStudent.size()));
        }
        session.setAttribute("students", targetStudent);
    }

    /**
     * @return String
     * @see list page
     */
    public String updateStudents() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        targetStudent = (List<Students>) session.getAttribute("students");
        Courses c = (Courses) session.getAttribute("editCourse");
        hbTransaction = hbSession.beginTransaction();
        try {
            for (Students s : targetStudent) {
                c.getStudentses().add(s);
                c = (Courses) hbSession.merge(c);
                hbSession.update(c);
                s = (Students) hbSession.merge(s);
                hbSession.update(s);
            }
            hbTransaction.commit();
            return "list";
        } catch (Exception e) {
            hbTransaction.rollback();
        }
        showPrimeMessage("Success", "Updated");
        return null;
    }

    /**
     * @param Students a student for adding choosen students with some courses
     * @return void
     */
    public void add(Students s) {
        HttpSession session = getHttpSession();
        currCourse = (Courses) getHttpSession().getAttribute("pickCourse");
        List<Students> listS = (List<Students>) getHttpSession().getAttribute("listS");
        sourseStudent.remove(s);
        targetStudent.add(s);
        if (listS == null) {
            listS = new ArrayList<Students>();
        }
//        for (Students students : targetStudent) {
//            if (students.getStudentId() == s.getStudentId()) {
//                targetStudent.remove(s);
//            }
//        }
        if (!isStudentExistInCourse(s, currCourse)) {
            listS.add(s);
        }
        session.setAttribute("listS", listS);
    }

    /**
     * @param Students a student for removing choosen students from some courses
     * @return void
     */
    public void remove(Students s) {
        List<Students> listR = (List<Students>) getHttpSession().getAttribute("listR");
        if (listR == null) {
            listR = new ArrayList<Students>();
        }
//        targetStudent.remove(s);
//        sourseStudent.add(s);
        listR.add(s);
        targetStudent.remove(s);
        sourseStudent.add(s);
        getHttpSession().setAttribute("listR", listR);
    }

    /**
     * @return String
     * @see pickstudent
     */
    public String update() {
        try {
            HttpSession session = getHttpSession();
            hbTransaction = hbSession.beginTransaction();
            List<Students> listR = (List<Students>) getHttpSession().getAttribute("listR");
            if (listR != null) {
                for (Students s : listR) {
                    currCourse = (Courses) getHttpSession().getAttribute("pickCourse");
                    currCourse.getStudentses().remove(s);

                    currCourse = (Courses) hbSession.merge(currCourse);
                    hbSession.update(currCourse);
                    s = (Students) hbSession.merge(s);
                    hbSession.update(s);

                }
//                 hbTransaction.commit();
            }
            List<Students> listS = (List<Students>) getHttpSession().getAttribute("listS");
            if (listS == null) {
                listS = new ArrayList<Students>();
            }
            Courses c = (Courses) session.getAttribute("pickCourse");
            for (Students s : listS) {

                c.getStudentses().add(s);
                c = (Courses) hbSession.merge(c);
                hbSession.update(c);
                s = (Students) hbSession.merge(s);
                hbSession.update(s);
            }

            hbTransaction.commit();
            loadData();
            return "pickstudent";
        } catch (Exception e) {
            hbTransaction.rollback();
            return null;
        }
    }

    /**
     * @param Students a student for checking whether he/she exist or not in the course
     * @param Courses a course for checking student's existence in it
     * @return String
     */
    boolean isStudentExistInCourse(Students s, Courses c) {
        List<Students> listS = Converter.iteratorToList(c.getStudentses().iterator());
        boolean e = false;
        for (Students students : listS) {
            if (s.getStudentId() == students.getStudentId()) {
                e = true;
                break;
            }
        }
        return e;
    }
}
